package com.mydemo.game.resaultStage;

import com.mydemo.entity.Car;
import com.mydemo.entity.Driver;
import com.mydemo.entity.Save;

import java.util.ArrayList;
import java.util.List;

public class DriverDaysCalculate {
    public static final Save driverDaysCalculate(Save save) {
        List<Driver> driverListCopy = save.getDriverList(); // 创建副本
        if (driverListCopy != null) {
            for (Driver driver : driverListCopy) {
                driver.setDays(driver.getDays() - 1);
                if (driver.getDays() < 5) {
                    if (driver.getDays() <= 0) {
                        System.out.println(driver.getName() + "租期到了，自动离职了");
                        if (save.getCarList() != null) {
                            for (Car car : save.getCarList()) {
                                if (driver.getCar() != 0 && driver.getCar() == car.getId()) {
                                    car.setDrive(false);
                                }
                            }
                        }
                    } else {
                        System.out.println("++++++++++++++++++++");
                        System.out.print("Id:" + driver.getId());
                        System.out.println("司机：" + driver.getName());
                        System.out.println("↑ 的剩余日期不足五天，请续费");
                    }
                }
            }
            // 循环结束后，根据记录删除元素
            List<Driver> driversToRemove = new ArrayList<>();
            for (Driver driver : driverListCopy) {
                if (driver.getDays() <= 0) {
                    driversToRemove.add(driver);
                }
            }
            save.getDriverList().removeAll(driversToRemove);
        }
        return save;
    }
}
